﻿/*
 * File:   myType.h
 * Author: aple
 * version : v1.0.0
 * Created on 2014年7月5日, 下午5:51
 */

#ifndef MYTYPE_H
#define MYTYPE_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "stdint.h"

#ifndef bbool
#define bbool uint8_t
#endif


    typedef unsigned char				uu8;
    typedef unsigned short				uu16;
    typedef unsigned long				uu32;
    typedef unsigned long long			uu64;

    typedef signed char					ii8;
    typedef signed short				ii16;
    typedef signed long					ii32;
    typedef signed long long			ii64;

    typedef volatile unsigned char		uuv8;
    typedef volatile unsigned short		uuv16;
    typedef volatile unsigned long		uuv32;
    typedef volatile unsigned long long	uuv64;

    typedef volatile signed char		iiv8;
    typedef volatile signed short		iiv16;
    typedef volatile signed long		iiv32;
    typedef volatile signed long long	iiv64;

    typedef const unsigned char			uuc8;
    typedef const unsigned short		uuc16;
    typedef const unsigned long			uuc32;
    typedef const unsigned long long	uuc64;

    typedef const signed char			iic8;
    typedef const signed short			iic16;
    typedef const signed long			iic32;
    typedef const signed long long		iic64;


#ifndef ext_inrom
#define ext_inrom				const
#endif

#ifndef ext_code
#define ext_code				const
#endif

#ifndef EXT_TRUE            
#define EXT_TRUE				1
#endif

#ifndef EXT_FALSE                              
#define EXT_FALSE				0
#endif

#ifndef EXT_ENABLE
#define EXT_ENABLE				1
#endif

#ifndef EXT_DISABLE
#define EXT_DISABLE				0
#endif

#define DIS						0
#define ENA						1
#define PRG						2

// get memory data
#ifndef outpw
#define outpw(port,value)		*((volatile unsigned long *)(port)) = value
#define inpw(port)				(*((volatile unsigned long *)(port)))
#define outps(port,value)		*((volatile unsigned short *)(port)) = value
#define inps(port)				(*((volatile unsigned short *)(port)))
#define outpb(port,value)		*((volatile unsigned char *)(port)) = value
#define inpb(port)				(*((volatile unsigned char *)(port)))
#define outp32(port,value)		*((volatile unsigned long *)(port)) = value
#define inp32(port)				(*((volatile unsigned long *)(port)))
#define outp16(port,value)		*((volatile unsigned short *)(port)) = value
#define inp16(port)				(*((volatile unsigned short *)(port)))
#define outp8(port,value)		*((volatile unsigned char *)(port)) = value
#define inp8(port)				(*((volatile unsigned char *)(port)))
#endif

// 指定地址读写浮点数
#define inpf32(port)			(*((volatile float *)(port)))
#define outpf32(port,value)		*((volatile float *)(port)) = value
#define inpf64(port)			(*((volatile double *)(port)))
#define outfd64(port,value)		*((volatile double *)(port)) = value
#define inp64(port)				(*((volatile unsigned long long *)(port)))
#define outp64(port,value)		*((volatile unsigned long long *)(port)) = value

#define ext_mem64(port)			(*((volatile unsigned long long *)(port)))
#define ext_mem32(port)			(*((volatile unsigned long *)(port)))
#define ext_mem16(port)			(*((volatile unsigned short *)(port))) 
#define ext_mem8(port)			(*((volatile unsigned char *)(port)))

//类型合并分离
#define byte_lb(x)				(x & 0xFF)
#define byte_hb(x)				((x >> 8) & 0xFF)
#define byte_ub(x)				((x >> 16) & 0xFF)
#define byte_mb(x)				((x >> 24) & 0xFF)

#define word_lw(x)				(x & 0xFFFF)
#define word_hw(x)				((x >> 16) & 0xFFFF)

#define word_make(hb,lb)		((uint16_t)((uint8_t)(lb) | ((uint8_t)(hb) << 8)))
#define dword_make(hw,lw)		((uint32_t)((uint16_t)(lw) | ((uint16_t)(hw) << 16)))
#define dword_wmake(hw,lw)		((uint32_t)((uint16_t)(lw) | ((uint16_t)(hw) << 16)))
#define dword_bmake(mb,ub,hb,lb)	((uint32_t)(((uint8_t)(mb)<<24) | ((uint8_t)(ub)<<16) |((uint8_t)(hb)<<8) |((uint8_t)(lb)<<0)))

// 地址a是否边界对齐于x
#define is_align(a,x)			(0 == ((uint32_t)(a)) & ((x)-1))

//比较
#if !defined(ext_max)
#define ext_max(a, b)			(((a) > (b)) ? (a) : (b))
#endif

#if !defined(ext_min)
#define ext_min(a, b)			(((a) < (b)) ? (a) : (b))
#endif

#define ext_abs(x)				((x<0)?(-(x)):(x))

//交换
#define swap32(ap, bp)          \
	{             				\
            uint32_t temp = ap; \
            ap = bp;            \
            bp = temp;          \
	}

#define swap16(ap, bp)          \
	{                         	\
            uint16_t temp = ap; \
            ap = bp;            \
            bp = temp;          \
	}

#define swap08(ap, bp)         \
	{                          \
            uint8_t temp = ap; \
            ap = bp;           \
            bp = temp;         \
	}

//-位处理-
#ifndef SetBit
#define SetBit(VAR, Pos)		((VAR) |= (1ul << (Pos)))
#define ClrBit(VAR, Pos)		((VAR) &= ~(1ul << (Pos)))
#define TogBit(VAR, Pos)		((VAR) ^= (1ul << (Pos)))
#define GetBit(VAR, Pos)		(((VAR) >> Pos) & 1ul)
#endif

#ifndef set_bit
#define set_bit					SetBit
#define clr_bit					ClrBit
#define tog_bit					TogBit
#define get_bit					GetBit
#endif

#ifndef BIT_MASK
#define BIT_MASK(x)				(1UL<<x)
#endif

#ifndef BIT
#define BIT BIT_MASK
#endif

#ifndef BITS_MASK
#define BITS_MASK(m,n)			( (m>n)?( (UINT32_MAX>>(31-m)) & (UINT32_MAX << n)) : ( (UINT32_MAX>>(31-n)) & (UINT32_MAX << m)))
#endif

#ifndef BITS
#define BITS BITS_MASK
#endif

#ifndef get_bits
#define get_bits(val, m, n)		((m>n)?((val & BITS_MASK(m,n))>>n):((val & BITS_MASK(m,n))>>m))
#endif

#ifndef set_bits
#define set_bits(val, m, n)		(val |= BITS_MASK(m,n))
#endif

#ifndef clr_bits
#define clr_bits(val, m, n)		(val &= ~BITS_MASK(m,n))
#endif

#ifndef tog_bits
#define tog_bits(val, m, n)		(val ^= BITS_MASK(m,n))
#endif

// -范围检测-
#define rang_offset(val, set, offset)  (((val+offset) >= set) && (val <= (set + offset)))    
#define rang_check(val, min, max)      ((val >= min)&&(val <= max))     
#define rang_limit(val, min, max)      {if(val<min)val=min;if(val>max)val=max;}
#define ext_abs(x)                     ((x<0)?(-(x)):(x))

// mixed
#define array_len(x)			(sizeof(x)/sizeof(x[0]))

    /*UINT8 UINT16 UINT32 BITS */
    typedef union
    {
        uint8_t Val;
        uint8_t uVal;
        int8_t iVal;
        struct
        {
            uint8_t b0 : 1;
            uint8_t b1 : 1;
            uint8_t b2 : 1;
            uint8_t b3 : 1;
            uint8_t b4 : 1;
            uint8_t b5 : 1;
            uint8_t b6 : 1;
            uint8_t b7 : 1;
        } bits;
    } UINT8_VAL, UINT8_BITS;

    typedef union
    {
        uint16_t Val;
        uint16_t uVal;
        int16_t iVal;

        uint8_t v[2];
        struct
        {
            uint8_t LB;
            uint8_t HB;
        } byte;

        struct
        {
            uint16_t b0 : 1;
            uint16_t b1 : 1;
            uint16_t b2 : 1;
            uint16_t b3 : 1;
            uint16_t b4 : 1;
            uint16_t b5 : 1;
            uint16_t b6 : 1;
            uint16_t b7 : 1;
            uint16_t b8 : 1;
            uint16_t b9 : 1;
            uint16_t b10 : 1;
            uint16_t b11 : 1;
            uint16_t b12 : 1;
            uint16_t b13 : 1;
            uint16_t b14 : 1;
            uint16_t b15 : 1;
        } bits;
    } UINT16_VAL, UINT16_BITS;

    typedef union
    {
        uint32_t Val;
        uint32_t uVal;
        int32_t iVal;

        uint16_t w[2];
        uint8_t v[4];

        struct
        {
            uint8_t LB;
            uint8_t HB;
            uint8_t UB;
            uint8_t MB;
        } byte;

        struct
        {
            uint16_t LW;
            uint16_t HW;
        } word;

        struct
        {
            uint32_t b0 : 1;
            uint32_t b1 : 1;
            uint32_t b2 : 1;
            uint32_t b3 : 1;
            uint32_t b4 : 1;
            uint32_t b5 : 1;
            uint32_t b6 : 1;
            uint32_t b7 : 1;
            uint32_t b8 : 1;
            uint32_t b9 : 1;
            uint32_t b10 : 1;
            uint32_t b11 : 1;
            uint32_t b12 : 1;
            uint32_t b13 : 1;
            uint32_t b14 : 1;
            uint32_t b15 : 1;
            uint32_t b16 : 1;
            uint32_t b17 : 1;
            uint32_t b18 : 1;
            uint32_t b19 : 1;
            uint32_t b20 : 1;
            uint32_t b21 : 1;
            uint32_t b22 : 1;
            uint32_t b23 : 1;
            uint32_t b24 : 1;
            uint32_t b25 : 1;
            uint32_t b26 : 1;
            uint32_t b27 : 1;
            uint32_t b28 : 1;
            uint32_t b29 : 1;
            uint32_t b30 : 1;
            uint32_t b31 : 1;
        } bits;
    } UINT32_VAL, UINT32_BITS;


    /*BYTE WORD DWORD BITS */
    typedef union
    {
        uint8_t Val;
        uint8_t uVal;
        int8_t iVal;
		uint8_t uval;
        int8_t ival;
        struct
        {
            uint8_t b0 : 1;
            uint8_t b1 : 1;
            uint8_t b2 : 1;
            uint8_t b3 : 1;
            uint8_t b4 : 1;
            uint8_t b5 : 1;
            uint8_t b6 : 1;
            uint8_t b7 : 1;
        } bits;
    } BYTE_VAL, BYTE_BITS, byte_tt;

    typedef union
    {
        uint16_t Val;
        uint16_t uVal;
        int16_t iVal;
		uint16_t uval;
        int16_t ival;
        uint8_t v[2];
        struct
        {
            byte_tt LB;
            byte_tt HB;
        } byte;

        struct
        {
            uint16_t b0 : 1;
            uint16_t b1 : 1;
            uint16_t b2 : 1;
            uint16_t b3 : 1;
            uint16_t b4 : 1;
            uint16_t b5 : 1;
            uint16_t b6 : 1;
            uint16_t b7 : 1;
            uint16_t b8 : 1;
            uint16_t b9 : 1;
            uint16_t b10 : 1;
            uint16_t b11 : 1;
            uint16_t b12 : 1;
            uint16_t b13 : 1;
            uint16_t b14 : 1;
            uint16_t b15 : 1;
        } bits;
    } WORD_VAL, WORD_BITS, word_tt;

    typedef union
    {
        uint32_t Val;
        uint32_t uVal;
        int32_t iVal;
		uint32_t uval;
        int32_t ival;
        uint16_t w[2];
        uint8_t v[4];

        struct
        {
            byte_tt LB;
            byte_tt HB;
            byte_tt UB;
            byte_tt MB;
        } byte;

        struct
        {
            word_tt LW;
            word_tt HW;
        } word;

        struct
        {
            uint32_t b0 : 1;
            uint32_t b1 : 1;
            uint32_t b2 : 1;
            uint32_t b3 : 1;
            uint32_t b4 : 1;
            uint32_t b5 : 1;
            uint32_t b6 : 1;
            uint32_t b7 : 1;
            uint32_t b8 : 1;
            uint32_t b9 : 1;
            uint32_t b10 : 1;
            uint32_t b11 : 1;
            uint32_t b12 : 1;
            uint32_t b13 : 1;
            uint32_t b14 : 1;
            uint32_t b15 : 1;
            uint32_t b16 : 1;
            uint32_t b17 : 1;
            uint32_t b18 : 1;
            uint32_t b19 : 1;
            uint32_t b20 : 1;
            uint32_t b21 : 1;
            uint32_t b22 : 1;
            uint32_t b23 : 1;
            uint32_t b24 : 1;
            uint32_t b25 : 1;
            uint32_t b26 : 1;
            uint32_t b27 : 1;
            uint32_t b28 : 1;
            uint32_t b29 : 1;
            uint32_t b30 : 1;
            uint32_t b31 : 1;
        } bits;
    } DWORD_VAL, DWORD_BITS, dword_tt;

    typedef union
    {
        uint8_t val;
        struct
        {
            uint8_t A : 1;
            uint8_t B : 1;
            uint8_t C : 1;
            uint8_t D : 1;
            uint8_t E : 1;
            uint8_t F : 1;
            uint8_t G : 1;
            uint8_t DP : 1;
        } seg;
    } led_seg_type;

#ifdef __cplusplus
}
#endif

#endif /* MYTYPE_H */
